﻿namespace FluentDataContract.Adapters.Json
{
    using System;
    using Newtonsoft.Json.Serialization;

    public sealed class FluentContractResolver : DefaultContractResolver
    {
        private JsonContractFactory _contractFactory;
        private JsonContractFactory ContractFactory { get { return _contractFactory ?? (_contractFactory = new JsonContractFactory()); } }

        public override JsonContract ResolveContract(Type type)
        {
            var contract = base.ResolveContract(type);
            if(contract is JsonObjectContract)
                contract = ContractFactory.ModifyExistingContract(type, (JsonObjectContract)contract);
            
            return contract;
        }

        public JsonContract What(JsonContract contract)
        {
            var result = contract;
            return result;
        }
    }
}